﻿using System;
using System.Collections;
using System.Collections.Generic;
using ShureValidation.Language;
using ShureValidation.Rules;

namespace ShureValidation
{
    /// <summary>
    /// Collection of validation errors.
    /// </summary>
    [Serializable]
    public class ValidationErrors : IEnumerable<ValidationError>
    {
        private readonly List<ValidationError> _errors = new List<ValidationError>();

        /// <summary>
        /// Remove all errors.
        /// </summary>
        public void Clear()
        {
            _errors.Clear();
        }

        /// <summary>
        /// Add a bunch of errors
        /// </summary>
        /// <param name="errors">Errors to add</param>
        public void Add(ValidationErrors errors)
        {
            _errors.AddRange(errors._errors);
        }
        /// <summary>
        /// Add a custom error message
        /// </summary>
        /// <param name="propertyName">Property name (not translated)</param>
        /// <param name="rule">Rule that didn't validate.</param>
        public void Add(string propertyName, IRule rule)
        {
            _errors.Add(new ValidationError(propertyName, rule));
        }

        /// <summary>
        /// Add a custom error message
        /// </summary>
        /// <param name="propertyName">Property name (not translated)</param>
        /// <param name="errorText">Error text, including {0} for translated property name.</param>
        /// <remarks>
        /// </remarks>
        /// <example>
        /// <code>
        /// errors.Add("Animal", "'{0}' är inte en get.");
        /// </code>
        /// </example>
        public void Add(string propertyName, string errorText)
        {
            _errors.Add(new ValidationError(propertyName, new TranslationRule(errorText)));
        }


        /// <summary>
        /// Gets number of errors.
        /// </summary>
        public int Count
        {
            get { return _errors.Count; }
        }

        #region IEnumerable<ValidationError> Members

        /// <summary>
        /// Returns an enumerator that iterates through the collection.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
        /// </returns>
        /// <filterpriority>1</filterpriority>
        public IEnumerator<ValidationError> GetEnumerator()
        {
            return _errors.GetEnumerator();
        }

        /// <summary>
        /// Returns an enumerator that iterates through a collection.
        /// </summary>
        /// <returns>
        /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
        /// </returns>
        /// <filterpriority>2</filterpriority>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        #endregion
    }

    /// <summary>
    /// Only used to translate custom error messages.
    /// </summary>
    [Serializable]
    internal class TranslationRule : IRule
    {
        private readonly string _formatting;

        /// <summary>
        /// Initializes a new instance of the <see cref="TranslationRule"/> class.
        /// </summary>
        /// <param name="formatting">Translated text, excluding property name.</param>

        public TranslationRule(string formatting)
        {
            _formatting = formatting;
        }

        /// <summary>
        /// Format an error message
        /// </summary>
        /// <param name="fieldName">Field (have already been translated)</param>
        /// <param name="rulesLanguage">Language prompts for validation rules</param>
        /// <returns>Error message formatted for the current language.</returns>
        public string Format(string fieldName, IModelLanguage rulesLanguage)
        {
            return string.Format(_formatting, fieldName);
        }

        /// <summary>
        /// Checks if this rule support values of the specified format.
        /// </summary>
        /// <param name="type">Type of value</param>
        /// <returns><c>true</c> if the specified type can be validated; otherwise <c>false</c>.</returns>
        public bool SupportsType(Type type)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Validate a field value
        /// </summary>
        /// <param name="value">Value to validate</param>
        /// <returns><c>true</c> if validation was successful; otherwise <c>false</c>.</returns>
        public bool Validate(object value)
        {
            throw new NotImplementedException();
        }
}
}